package com.TestDrawCurve;
   
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class TestDrawCurveActivity extends Activity implements View.OnClickListener,
	OnTouchListener,OnGestureListener,GraphView.initViewListener,
	GraphSurfaceView.initSurViewListener{
    /** Called when the activity is first created. */
	private List<GraphView>  mListGV;
	private LinearLayout mLLCurveWrap; 
	private List<GraphSurfaceView>  mListSurGV;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //mGraphView = new GraphView(this);
        setContentView(R.layout.main);
        
        Button btnAddCurve = (Button)findViewById(R.id.BTN_AddCurve);
        btnAddCurve.setOnClickListener(this);
        Button btnReduceCurve = (Button)findViewById(R.id.BTN_ReduceCurve);
        btnReduceCurve.setOnClickListener(this);
        Button btnChangeCurve = (Button)findViewById(R.id.BTN_ChangeCurve);
        btnChangeCurve.setOnClickListener(this);
        
        Button btnSurCurve = (Button)findViewById(R.id.BTN_AddSurCurve);
        btnSurCurve.setOnClickListener(this);
        Button btnChangeSurCurve = (Button)findViewById(R.id.BTN_ChangeSurCurve);
        btnChangeSurCurve.setOnClickListener(this);
        mLLCurveWrap = (LinearLayout)findViewById(R.id.LL_CurveWrap);
        
        
        mListGV = new ArrayList<GraphView>();
        mListSurGV = new ArrayList<GraphSurfaceView>();
        
        
    }
 
    public int changeheight=10;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
		case R.id.BTN_AddCurve:
			GraphView tmp = new GraphView(this);
			tmp.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT,1));
			tmp.setOnInitViewListener(this);
			mListGV.add(tmp);
			mLLCurveWrap.addView(tmp);
			

			
			break;
		case R.id.BTN_AddSurCurve:
			
			GraphSurfaceView tmp2 = new GraphSurfaceView(this);
			tmp2.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT,1));
			tmp2.setOnInitSurViewListener(this);
			mListSurGV.add(tmp2);
			mLLCurveWrap.addView(tmp2);
			break;
		case R.id.BTN_ReduceCurve:
			if(mListGV.size()>0)
			{
				mLLCurveWrap.removeView(mListGV.get(mListGV.size()-1));
				mListGV.remove(mListGV.size()-1);
			}

			break;
		case R.id.BTN_ChangeCurve:
			List<Float> data1 = new ArrayList<Float>();
			for(int i=0;i<512;i++)
	        { 
				float height=10;
				if(i<200)
				{
					changeheight+=10;
					height = changeheight;
				}
	        	float value = (float) Math.sin(i*2*Math.PI/180)*height;
	        	data1.add(value);
	        } 
			for(int i=0;i<mListGV.size();i++)
			{ 
				GraphView v2 = (GraphView) mLLCurveWrap.getChildAt(i);
				v2.setCurveData(data1);
			}
			break;
		case R.id.BTN_ChangeSurCurve:
			List<Float> data = new ArrayList<Float>();
			for(int i=0;i<512;i++)
	        { 
				float height=10;
				if(i<200)
				{
					changeheight+=10;
					height = changeheight;
				}
	        	float value = (float) Math.sin(i*2*Math.PI/180)*height;
	        	data.add(value);
	        } 
			for(int i=0;i<mListSurGV.size();i++)
			{ 
				GraphSurfaceView v2 = (GraphSurfaceView) mLLCurveWrap.getChildAt(i);
				v2.setCurveData(data);
			}
			break;
		}

		
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGesture(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onSizeChange() {
		// TODO Auto-generated method stub
		List<Float> data = new ArrayList<Float>();
		for(int i=0;i<512;i++)
        { 
			float height=50;
			if(i<200)
			{
				height = 10;
			}
        	float value = (float) Math.sin(i*2*Math.PI/180)*height;
        	data.add(value);
        } 
		for(int i=0;i<mListGV.size();i++)
		{ 
			GraphView v = (GraphView) mLLCurveWrap.getChildAt(i);
			v.setCurveData(data);
		}
		
		for(int i=0;i<mListSurGV.size();i++)
		{ 
			GraphSurfaceView v = (GraphSurfaceView) mLLCurveWrap.getChildAt(i);
			v.setCurveData(data);
		}		
		
	}
    
 
}